import java.sql.*;

public class Function {
    public void BuyTicket(){
        String url = "jdbc:jtds:sqlserver://localhost:1433/AirportData";
        String user = "sa";
        String password = "hui1234567890";

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("数据库连接成功！");
            // 测试查询 - 使用不同的列名避免关键字冲突
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT f.Flight_Number,Departure_time,Arrival_time,Departure_airport,Arrival_airport,Ticket_price from Flights f join flightTicket ft on f.Flight_id = ft.Flight_id");
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            int num = 1001;
            System.out.println("--------购票--------");
            while (rs.next()) {
                System.out.println("-------------"+num+"-------------");
                System.out.println(rs.getString(2) + "\t\t"+rs.getString(3));
                System.out.println(rs.getString(4) + "=======>>"+rs.getString(5)+"\t"+"￥"+rs.getString(6));
                System.out.println(rs.getString(1));
                System.out.println("------------------------------");
                num++;
            }


        } catch (SQLException e) {
            System.out.println("数据库操作失败！");
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                    System.out.println("数据库连接已关闭");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
